package hieudao.exercise_collection.models;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import hieudao.exercise_collection.entity_data.Book;
import hieudao.exercise_collection.entity_data.BookManager;
import hieudao.exercise_collection.entity_data.Reader;

public class LibraryManagementModel {
	private List<Reader> lstReader;
	private List<Book> lstBook;
	private List<BookManager> lstBookManager;

	// This is function to execute input Book
	public void inputBookInfo(Book book) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		lstBook = new ArrayList<Book>();
		long bookID = 10000;
		int i = 0;
		int specializedValue;
		String exitValue = "";
		while (!exitValue.equalsIgnoreCase("N")) {
			System.out.println("Please, You enter information book " + ++i + ": ");
			System.out.println("You must select a specialized the following: ");
			System.out.println("1. Natural Sciences");
			System.out.println("2. Literature and Art");
			System.out.println("3. Electronics and Telecommunication");
			System.out.println("4. Information Technology");

			specializedValue = Integer.parseInt(bf.readLine());
			if ((specializedValue == 1) || (specializedValue == 2) || (specializedValue == 3)
					|| (specializedValue == 4)) {
				book = new Book();
				switch (specializedValue) {
				case 1: {
					book.setSpecialized("Natural Sciences");
					book.setBookID(bookID++);
					System.out.println("Book's name: ");
					book.setBookName(bf.readLine());
					System.out.println("Author's name: ");
					book.setAuthorName(bf.readLine());
					System.out.println("Publishing year: ");
					book.setPublishYear(Integer.parseInt(bf.readLine()));
					lstBook.add(book);
					break;
				}
				case 2: {
					book.setSpecialized("Literature and Art");
					book.setBookID(bookID++);
					System.out.println("Book's name: ");
					book.setBookName(bf.readLine());
					System.out.println("Author's name: ");
					book.setAuthorName(bf.readLine());
					System.out.println("Publishing year: ");
					book.setPublishYear(Integer.parseInt(bf.readLine()));
					lstBook.add(book);
					break;
				}
				case 3: {
					book.setSpecialized("Electronics and Telecommunication");
					book.setBookID(bookID++);
					System.out.println("Book's name: ");
					book.setBookName(bf.readLine());
					System.out.println("Author's name: ");
					book.setAuthorName(bf.readLine());
					System.out.println("Publishing year: ");
					book.setPublishYear(Integer.parseInt(bf.readLine()));
					lstBook.add(book);
					break;
				}
				case 4: {
					book.setSpecialized("Information Technology");
					book.setBookID(bookID++);
					System.out.println("Book's name: ");
					book.setBookName(bf.readLine());
					System.out.println("Author's name: ");
					book.setAuthorName(bf.readLine());
					System.out.println("Publishing year: ");
					book.setPublishYear(Integer.parseInt(bf.readLine()));
					lstBook.add(book);
					break;
				}
				}
				System.out.println("You enter \"y\" to continue or \"n\" on keyboard to exit system");
				exitValue = bf.readLine();
			} else {
				System.out.println("You just enter specialized invalid! Please you try again");
				System.out.println("You enter \"y\" to continue or \"n\" on keyboard to exit system");
				exitValue = bf.readLine();
			}
		}
	}

	// This is function to display book
	public void displayBookInfo(List<Book> lstBook) {
		int i = 0;
		System.out.println("Infomation about book in laibarary include of: ");
		if (lstBook.size() > 0) {
			for (Book book : lstBook) {
				System.out.println("Infomation book [" + ++i + "]");
				System.out.println("Code of Book: " + book.getBookID());
				System.out.println("Book name: " + book.getBookName());
				System.out.println("Specialized : " + book.getSpecialized());
				System.out.println("Author name: " + book.getAuthorName());
				System.out.println("Publish year: " + book.getPublishYear());
				System.out.println("----------------------------------------------------------\n");
			}
		} else {
			System.out.println("The system does not has book exist");
		}
	}

	// This is function to input readers
	public void inputReaderInfo(Reader reader) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		lstReader = new ArrayList<Reader>();
		System.out.println("The system is running");
		long readerID = 10000;
		int i = 0;
		int readerTypeValue;
		String exitValue = "";
		while (!exitValue.equalsIgnoreCase("N")) {
			System.out.println("Please, You enter information readers " + ++i + ": ");
			System.out.println("You must select a type of readers the following: ");
			System.out.println("1. Studen");
			System.out.println("2. Master Student");
			System.out.println("3. Teacher");

			readerTypeValue = Integer.parseInt(bf.readLine());
			if ((readerTypeValue == 1) || (readerTypeValue == 2) || (readerTypeValue == 3)) {
				reader = new Reader();
				switch (readerTypeValue) {
				case 1: {
					reader.setReaderType("Studen");
					reader.setReaderID(readerID++);
					System.out.println("Reader's name: ");
					reader.setFullName(bf.readLine());
					System.out.println("Reader's phone number: ");
					reader.setPhoneNumber(Long.parseLong(bf.readLine()));
					System.out.println("Reader's address: ");
					reader.setAddress(bf.readLine());
					lstReader.add(reader);
					break;
				}
				case 2: {
					reader.setReaderType("Master Student");
					reader.setReaderID(readerID++);
					System.out.println("Reader's name: ");
					reader.setFullName(bf.readLine());
					System.out.println("Reader's address: ");
					reader.setAddress(bf.readLine());
					System.out.println("Reader's phone number: ");
					reader.setPhoneNumber(Long.parseLong(bf.readLine()));
					lstReader.add(reader);
					break;
				}
				case 3: {
					reader.setReaderType("Teacher");
					reader.setReaderID(readerID++);
					System.out.println("Reader's name: ");
					reader.setFullName(bf.readLine());
					System.out.println("Reader's address: ");
					reader.setAddress(bf.readLine());
					System.out.println("Reader's phone number: ");
					reader.setPhoneNumber(Long.parseLong(bf.readLine()));
					lstReader.add(reader);
					break;
				}
				}
				System.out.println("You enter \"y\" to continue or \"n\" on keyboard to exit system");
				exitValue = bf.readLine();
			} else {
				System.out.println("You just enter specialized invalid! Please you try again");
				System.out.println("You enter \"y\" to continue or \"n\" on keyboard to exit system");
				exitValue = bf.readLine();
			}
		}
	}

	// This is function to display readers
	public void displayReaderInfo(List<Reader> lstReader) {
		int i = 0;
		System.out.println("Infomation about reader in laibarary include of: ");
		if (lstReader.size() > 0) {
			for (Reader reader : lstReader) {
				System.out.println("Infomation reader [" + ++i + "]");
				System.out.println("Reader's type: " + reader.getReaderType());
				System.out.println("Reader's id: " + reader.getReaderID());
				System.out.println("Reader's name: " + reader.getFullName());
				System.out.println("Reader's phone : " + reader.getPhoneNumber());
				System.out.println("Reader's address : " + reader.getAddress());
				System.out.println("----------------------------------------------------------\n");
			}
		} else {
			System.out.println("The system does not has reader exist");
		}
	}

	// This is function management rent book
	public void inputReaderBookInfo(BookManager bookManager) throws NumberFormatException, IOException {
		lstBookManager = new ArrayList<BookManager>();
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		long tmpReaderID;
		long tmpBookID;
		boolean checkReaderIDExist = false;
		boolean checkBookIDExist = false;
		String exitValue = "";
		do {
			bookManager = new BookManager();
			System.out.println("You enter reader ID");
			tmpReaderID = Long.parseLong(bf.readLine());

			for (Reader reader : lstReader) {
				if (tmpReaderID == reader.getReaderID()) {
					bookManager.getReader().setReaderID(tmpReaderID);
					bookManager.getReader().setFullName(reader.getFullName());
					bookManager.getReader().setReaderType(reader.getReaderType());
					bookManager.getReader().setPhoneNumber(reader.getPhoneNumber());
					bookManager.getReader().setAddress(reader.getAddress());
					checkReaderIDExist = true;
					checkBookIDExist = false;
				}
			}
			if (checkReaderIDExist) {
				checkReaderIDExist = false;
				System.out.println("You enter book ID: ");
				tmpBookID = Long.parseLong(bf.readLine());
				for (Book book : lstBook) {
					if (tmpBookID == book.getBookID()) {
						bookManager.getBook().setBookID(tmpBookID);
						bookManager.getBook().setBookName(book.getBookName());
						bookManager.getBook().setSpecialized(book.getSpecialized());
						bookManager.getBook().setAuthorName(book.getAuthorName());
						bookManager.getBook().setPublishYear(book.getPublishYear());
						checkBookIDExist = true;
					}
				}
				if (checkBookIDExist) {
					lstBookManager.add(bookManager);
					System.out.println(
							"Do you want to add more? You enter \"y\" to continue or \"n\" on the keyboard to exit");
					exitValue = bf.readLine();
				} else {
					System.out
							.println("Book ID not exist! You enter \"y\" to continue or \"n\" on the keyboard to exit");
					exitValue = bf.readLine();
				}
			} else {
				System.out.println("Reader ID not exist! You enter \"y\" to continue or \"n\" on the keyboard to exit");
				exitValue = bf.readLine();
			}
		} while (!exitValue.equalsIgnoreCase("N"));
	}

	public void displayReaderBookInfo(List<BookManager> lstBookManager) {
		System.out.println("This is information of reader book");
		for (int i = 0; i < lstBookManager.size(); i++) {
			System.out.println("Information of rent book [" + i + "]");
			System.out.println("Rent reader ID: " + lstBookManager.get(i).getReader().getReaderID());
			System.out.println("Rent reader ID: " + lstBookManager.get(i).getReader().getFullName());
			System.out.println("Rent reader ID: " + lstBookManager.get(i).getReader().getReaderType());
			System.out.println("Rent reader ID: " + lstBookManager.get(i).getBook().getBookID());
			System.out.println("Rent name: " + lstBookManager.get(i).getBook().getBookName());
			System.out.println("Rent book ID: " + lstBookManager.get(i).getBook().getSpecialized());
			System.out.println("---------------------------------------------------------------------------------");
		}
	}

	/**
	 * @return the lstBook
	 */
	public List<Book> getLstBook() {
		return lstBook;
	}

	/**
	 * @param lstBook
	 *            the lstBook to set
	 */
	public void setLstBook(List<Book> lstBook) {
		this.lstBook = lstBook;
	}

	/**
	 * @return the lstReader
	 */
	public List<Reader> getLstReader() {
		return lstReader;
	}

	/**
	 * @param lstReader
	 *            the lstReader to set
	 */
	public void setLstReader(List<Reader> lstReader) {
		this.lstReader = lstReader;
	}

	/**
	 * @return the lstBookManager
	 */
	public List<BookManager> getLstBookManager() {
		return lstBookManager;
	}

	/**
	 * @param lstBookManager
	 *            the lstBookManager to set
	 */
	public void setLstBookManager(List<BookManager> lstBookManager) {
		this.lstBookManager = lstBookManager;
	}

}
